import { app, ipcMain, dialog } from 'electron';
import _ from 'lodash';
import path from 'path';
import ThreadController from './scancontrol';
import Configuration from './config';

export default function() {
    ipcMain.on('select-directory', (e, arg) => {
        dialog.showOpenDialog({ properties: ['openDirectory'] }, dirs => {
            if (_.isUndefined(dirs) && _.isEmpty(dirs)) {
                return;
            }
            e.sender.send('directory-selected', dirs[0]);
            ThreadController.restartThread();
            ThreadController.scanPath(dirs[0]);
        });
    });

    ipcMain.on('select-image', (e, arg) => {
        ThreadController.Sender = e.sender;
        ThreadController.selectOnePicture();
    });

    ipcMain.on('save-config', (e, arg) => {
        Configuration.config = arg;
        ThreadController.setShuffle(Configuration.config.shuffle);
        Configuration.save();
    });

    ipcMain.on('save-new-pin', (e, arg) => {
        Configuration.config.pin = arg.pin;
        Configuration.save();
    });

    ipcMain.on('quit-app', (e, arg) => {
        ThreadController.exitThread();
        app.quit();
    });
}
